<?php
namespace app\tripapi\controller;
use app\tripapi\model\TravelPlanItem;
use app\tripapi\model\TravelPlan;
use app\tripapi\model\TravelChat;
use app\tripapi\model\TravelApply;
use app\tripapi\model\TravelApplyCustomer;
use app\tripapi\model\TravelChatProduct;
use think\Controller;
use think\Request;
class Index extends ApiBaseController
{
//    private $attachFile;
    private $TravelPlanItem;
    private $TravelPlan;
    private $TravelChat;
    private $TravelChatProduct;
    private $TravelApply;
    private $TravelApplyCustomer;

    public function __construct()
    {
        parent::__construct();
        $this->departmentid = $_SESSION['LOGIN_DEPT_ID'];
        $this->uid = $_SESSION['LOGIN_UID']; // 填报人id
        $this->uname = str_replace(',', '', iconv('gbk', 'UTF-8', GetUserNameByUid($this->uid))); // 填报人姓名
//        $this->attachFile = new AttachFile();
        $this->TravelPlanItem = new TravelPlanItem();
        $this->TravelPlan = new TravelPlan();
        $this->TravelChat = new TravelChat();
        $this->TravelApply = new TravelApply();
        $this->TravelApplyCustomer = new TravelApplyCustomer();
        $this->TravelChatProduct = new TravelChatProduct();
    }

    // 自己的列表页
    public function index()
    {
        return $this->fetch();
    }

    // 部门经理列表页--审批
    public function departmentlist()
    {
        return $this->fetch();
    }

    // 总经理列表页 -- 审批
    public function managerlist()
    {
        return $this->fetch();
    }

    public function travelnegotiation()
    {
        return $this->fetch();
    }

    // 列表页API接口
    public function GetApplyListByAjax()
    {
        $result = $this->TravelPlan->GetApplyList($this->uid);
        foreach ($result as $key => $value) {
            $result[$key]['department'] = iconv('GBK', 'UTF-8', $value['department']);
            $result[$key]['user_name'] = iconv('GBK', 'UTF-8', $value['user_name']);
            $result[$key]['creator'] = iconv('GBK', 'UTF-8', $value['creator']);
        }
        $count = count($result);
        $json = array("status" => 200, "msg" => "获取成功", "count" => $count, "data" => $result);
        return json_api($json);
    }

    // 出差申请单列表
    public function GetApplyCustomerListByAjax()
    {
        $travelplan = $this->TravelPlan->GetDetail($this->params['id']);
        $travel_plan = $this->TravelApply->GetApplyId($travelplan['0']['plan_num']); // 获取apply id
        $count = count($travel_plan);
        $json = array("status" => 200, "msg" => "获取成功", "count" => $count, "data" => $travel_plan);
        return json_api($json);
    }

    // 月度计划表单据添加
    public function travelplan()
    {
        $this->assign('department', $this->uname);
        return $this->fetch();
    }

    // 查看月度计划表单据
    public function travelplanshow()
    {
        $id = $_GET['id'];
        $result = $this->TravelPlanItem->GetList($id);
        $this->assign('itemlist', $result);
        return $this->fetch();
    }

    // 编辑月度计划表单据
    public function travelplanedit()
    {
        $id = $_GET['id'];
        $travelplan = $this->TravelPlan->GetDetail($id);
        $this->assign('plandetail', $travelplan);
        $result = $this->TravelPlanItem->GetList($id);
        $this->assign('itemdetail', $result);
        return $this->fetch();
    }

    // 添加页
    public function traveladd()
    {
        $travel_num_list = $this->TravelPlan->where('user_id', '=', $_SESSION['LOGIN_UID'])->select();
        $this->assign('travel_num_list', $travel_num_list);
        return $this->fetch();
    }

    // 销售人员客户洽谈纪要
    public function travelsaler()
    {
        $travel_num_list = $this->TravelPlan->where('user_id', '=', $_SESSION['LOGIN_UID'])->select();
        $this->assign('travel_num_list', $travel_num_list);
        return $this->fetch();
    }

    // 非销售人员客户洽谈纪要
    public function travelnotsaler()
    {
        $travel_num_list = $this->TravelPlan->where('user_id', '=', $_SESSION['LOGIN_UID'])->select();
        $this->assign('travel_num_list', $travel_num_list);
        return $this->fetch();
    }

    // 查看月度单号对应的申请单
    public function travelshowapply()
    {
        return $this->fetch();
    }

    // 查看出差申请单明细
    public function travelitemshow()
    {
        $id = $_GET['id'];
        $result = $this->TravelApply->GetItemById($id);
        $this->assign('traffic_self', json_decode($result['0']['traffic_self']));
        $this->assign('business_receive', json_decode($result['0']['business_receive']));
        $this->assign('budget_item', json_decode($result['0']['budget_item']));
        $this->assign('itemdetail', $result);
        return $this->fetch();
    }

    // 编辑出差申请单明细
    public function travelitemedit()
    {
        $id = $_GET['id'];
        $result = $this->TravelApply->GetItemById($id);
        $this->assign('traffic_self', json_decode($result['0']['traffic_self']));
        $this->assign('business_receive', json_decode($result['0']['business_receive']));
        $this->assign('budget_item', json_decode($result['0']['budget_item']));
        $this->assign('itemdetail', $result);
        return $this->fetch();
    }

    // 生成唯一单号方法
    public function GetUniqueOrderId()
    {
        $prefix = "CC";
        $center = date('Ymd');
        $ymonth = date('Ym'); // 根据当前时间的年月 判定自增序列的条件 按年月度自增
        $lastid = $this->TravelPlan->field('plan_num')->order('id desc')->limit(1)->find();
        $last_plan_num = $lastid['plan_num'];
        if (strpos($last_plan_num, $ymonth) !== false) { // 根据当前时间的年月与数据库中最新一条记录的出差编号对比 若包含 则表示同月份 自增加1
            $fix = substr($last_plan_num, 0, strlen($last_plan_num) - 1) . chr(ord(substr($last_plan_num, -1, 1)) + 1); // 自增加1
        } else { // 若不包含 则从1开始自增
            $fix = str_pad($prefix . $center, 13, 0, STR_PAD_RIGHT) . '1'; // 向右侧添加000
        }
        return $fix;
    }

    // 月度出差计划保存
    public function PlanItemSave()
    {
//        print_r($this->params);exit;
        $this->params['user_id'] = $this->uid;
        $this->params['user_name'] = $this->uname;
        $this->params['department_id'] = $this->departmentid; // 填报部门id
        $this->params['plan_num'] = $this->GetUniqueOrderId(); // 月度出差计划单号
        $this->params['creator'] = $this->uname; // 创建人
        $this->params['creator_id'] = $this->uid; // 创建人id
        //存储 travel plan 总表
        $id = $this->TravelPlan->TravelPlanSave($this->params);
        if ($id) { // 若总表存储成功 继续保存明细表
            $this->TravelPlanItem->TravelPlanItemSave($id, $this->params);
            $retInfo = array("status" => 200, "msg" => "申请成功", "data" => array());
        } else { // 总表保存失败 提示错误 不做明细表存储
            $retInfo = array("status" => 500, "msg" => '申请失败', "data" => array());
        }
        return json_api($retInfo);
    }

    // 月度出差计划更新
    public function PlanItemEdit()
    {
        $this->params['user_id'] = $this->uid;
        $this->params['user_name'] = $this->uname;
        $this->params['department_id'] = $this->departmentid; // 填报部门id
        $this->params['plan_num'] = $this->GetUniqueOrderId(); // 月度出差计划单号
        $this->params['creator'] = $this->uname; // 创建人
        $this->params['creator_id'] = $this->uid; // 创建人id
        $this->TravelPlanItem->TravelPlanItemUpdate($this->params);
        $retInfo = array("status" => 200, "msg" => "更新成功", "data" => array());
        return json_api($retInfo);
    }

    // 出差申请表保存
    public function TravelApplySave()
    {
        $this->params['creator'] = $this->uname; // 创建人
        $this->params['creator_id'] = $this->uid; // 创建人id
        $id = $this->TravelApply->TravelApplySave($this->params);
        if ($id) {
            $this->TravelApplyCustomer->TravelApplyCustomerSave($id, $this->params);
            $retInfo = array("status" => 200, "msg" => '申请成功', "data" => array());
        } else {
            $retInfo = array("status" => 500, "msg" => '申请失败', "data" => array());
        }
        return json_api($retInfo);
    }

    //销售人员客户洽谈单
    public function TravelChatSave()
    {
        $this->params['creator'] = $this->uname; // 创建人
        $this->params['creator_id'] = $this->uid; // 创建人id
        $id = $this->TravelChat->TravelChatSave($this->params);
        if ($id) {
            $this->TravelChatProduct->TravelChatProductSave($id, $this->params);
            $retInfo = array("status" => 200, "msg" => '申请成功', "data" => array());
        } else {
            $retInfo = array("status" => 500, "msg" => '申请失败', "data" => array());
        }
        return json_api($retInfo);
    }

    // 非销售人员客户洽谈单
    public function TravelNotSalerChatSave()
    {
        $this->params['creator'] = $this->uname; // 创建人
        $this->params['creator_id'] = $this->uid; // 创建人id
        $id = $this->TravelChat->TravelNotSalerChatSave($this->params);
        if ($id) {
            $retInfo = array("status" => 200, "msg" => '申请成功', "data" => array());
        } else {
            $retInfo = array("status" => 500, "msg" => '申请失败', "data" => array());
        }
        return json_api($retInfo);
    }
}